#include "public.h"

int sd;

void *func(void *q)
{
	char sbuf[100];
	while (1)
	{
		read(sd, sbuf, sizeof(sbuf));
		printf("%s\n", sbuf);
	}
}

int funQuit(int s)
{
	printf("\n输入'quit'退出！\n");
}

int main()
{
	signal(SIGINT, funQuit);
	sd = socket(AF_INET, SOCK_STREAM, 0);
	if (sd == -1)
	{
		perror("socket failed!\n");
		exit(-1);
	}
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(1025);
	inet_aton("127.0.0.1", &addr.sin_addr);
	int ss = connect(sd, (struct sockaddr *)&addr, sizeof(addr));
	if (ss == -1)
	{
		perror("connect failed!\n");
		exit(-1);
	}
	char cbuf[100];
	char name[100];
	printf("请输入你的昵称：");
	gets(name);
	write(sd, name, sizeof(name));
	pthread_t tid;
	pthread_create(&tid, NULL, func, NULL);
	while (1)
	{
		gets(cbuf);
		write(sd, cbuf, sizeof(cbuf));
		if (strcmp(cbuf, "quit") == 0)
		{
			printf("你已退出聊天室");
			break;
		}
	}
}